package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.service.ReturnListGoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.atguigu.jxc.service.ReturnListGoodsService;
import com.atguigu.jxc.vo.GoodVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.ReturnList;
import com.atguigu.jxc.service.ReturnListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
import java.util.Map;

@RestController
@RequestMapping("/returnListGoods")
public class ReturnListGoodsController {

    @Autowired
    private ReturnListGoodsService returnListGoodsService;

    private static final ObjectMapper MAPPER = new ObjectMapper();


    @PostMapping("save")
    public ServiceVO save(ReturnList returnList, String returnListGoodsStr){
        this.returnListGoodsService.save(returnList, returnListGoodsStr);
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @PostMapping("/list")
    @RequiresPermissions("退货出库")
    public Map<String,Object> list(String returnNumber,Integer supplierId, Integer state,String sTime,String eTime){
        Map<String,Object> map= returnListGoodsService.list(returnNumber,supplierId,state,sTime,eTime);
        return map;
    }

    @PostMapping("/goodsList")
    @RequiresPermissions("退货出库")
    public Map<String,Object> goodsList(Integer returnListId){
        Map<String,Object> map=returnListGoodsService.goodsList(returnListId);
        return map;
    }

    @PostMapping("/delete")
    @RequiresPermissions("退货出库")
    public ServiceVO delete(Integer returnListId) {
        returnListGoodsService.delete(returnListId);
        return new ServiceVO(100, "请求成功");
    }


    /**
     * 退货统计（可根据 商品类别、商品编码或名称 条件查询）
     *
     * @param sTime
     * @param eTime
     * @param goodsTypeId
     * @param codeOrName
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("count")
    public String count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) throws JsonProcessingException {
        List<GoodVo> goodVos = this.returnListGoodsService.count(sTime, eTime, goodsTypeId, codeOrName);
        return MAPPER.writeValueAsString(goodVos);
    }
}
